// pages/slot-machine/index.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    csqye: '0',
  },

  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    spinDisabled: false,
    chou:true,
    result: [], // 中奖池
    z_yonghu: ''
  },

  methods: {
    z_yonghu() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/season/seasonCurInfo',
        method: 'GET',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            yonghu: res.data.data
          })
        }
      })
    },

    // 开始
    start() {
      let _this = this
      _this.setData({
        chou:false
      })
      _this.z_yonghu()
      setTimeout(function () {
        _this.start1()
      }, 300)
    },
    start1() {
      let _this = this
      const {
        spinDisabled
      } = _this.data
      // 点击开始后不可点击
      if (spinDisabled) return false
      // 随机设置奖项 该数据应该从后台接口获取
      let result = []
      // for (var i = 0; i < 3; i++) {
      //     result.push(Math.floor(Math.random() * 6 + 1))
      // }
      let params = {
        "token": wx.getStorageSync("user_token"),
        "season": _this.data.yonghu.season_num
      }
      wx.request({
        url: app.apiurl + 'api/member/luckCjNum',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          if(res.data.code == '1002'){
            wx.showToast({
              title: res.data.msg,
              icon: 'error',
              duration: 2000
            })
          } else {
            let num1 = res.data.data.num_info.num1;
            if(num1 == 'A'){
              num1 = 10
            }
            let num2 = res.data.data.num_info.num2;
            if(num2 == 'A'){
              num2 = 10
            } 
            let num3 = res.data.data.num_info.num3;
            if(num3 == 'A'){
              num3 = 10
            } 
            console.log('1111111111', num1, num2, num3)
            result.push(Number(num1) + 2, Number(num2) + 2, Number(num3) + 2)
            _this.setData({
              spinDisabled: true,
              result: result,
              luckCjNum: res.data.data
            })
            // 触发组件开始方法
            _this.selectComponent('#sol-slot-machine').start()
          }
        }
      })
    },
    // 结束
    doFinish() {
      let _this = this
      console.log('当前项=', this.data.result)
      _this.setData({
        spinDisabled: false,
        chou:true
      })
      //传输结果
      _this.triggerEvent('callBack', _this.data.luckCjNum);
      _this.z_yonghu()
    }
  }
})